1472E - Correct Placement - CodeForces Solution


binary search data structures dp sortings two pointers *1700

Please click on ads to support us..

Python Code:

T = int(input())
for t in range(T):
    n = int(input())
    lis = [0]*n
    ans = [-1]*n
    for i in range(n):
        x, y = map(int, input().split())
        lis[i] = (max(x, y), min(x, y), i)
    lis.sort()
    y2, i2 = 10**10, 0
    last_x = -1
    for x, y, i in lis:
        if last_x < x:
            min_y, min_j = y2, i2
            last_x = x
        if min_y < y:
            ans[i] = min_j+1
        if y < y2:
            y2, i2 = y, i
    print(*ans, sep=' ')

C++ Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll>pii;
typedef pair<pii,ll>ppi;
#define f first
#define s second
int main()
{
	int t;cin>>t;
	while(t--)
    {
        int n;cin>>n;
        vector<ppi>vc;
        for(int i=1;i<=n;i++)
        {
            ll x,y;cin>>x>>y;
            vc.push_back(ppi(pii(x,-y),i));
            vc.push_back(ppi(pii(y,-x),i));
        }
        sort(vc.begin(),vc.end());
        vector<ll>ans;
        for(int i=0;i<n;i++){ans.push_back(-1);}
        ll mn=LONG_MAX;
        ll el=-1;
        for(auto x:vc)
        {
            ll h=-x.f.s;
            ll i=x.s;
            if(h>mn){ans[i-1]=el;}
            else{mn=h;el=i;}
        }
        for(auto x:ans){cout<<x<<' ';}
        cout<<endl;
    }
}


Comments

Submit
0 Comments
More Questions

1651C - Fault-tolerant Network
870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro
780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions
1574C - Slay the Dragon
621A - Wet Shark and Odd and Even
1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones